package com.onion.utils.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    private static Logger log = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 本地文件是否存在
     * @Description:
     * @param localFile
     * @return
     * @Return:boolean
     * @Time:2017-6-15
     * @Author:Arno
     */
    public static boolean fileExist(String localFile){
        return new File(localFile).exists();
    }

    /**
     * 检索目录文件,不检索子目录
     * @param dir
     * @return
     */
    public static List<String> seekFiles(String dir){
        List<String> fileList = new ArrayList<String>(10);
        seekFiles(fileList, new File(dir), false);
        return fileList;
    }

    /**
     * 检索目录文件
     * @param dir
     * @param isSubDir：true-检索子目录
     * @return
     */
    public static List<String> seekFiles(String dir, boolean isSubDir){
        List<String> fileList = new ArrayList<String>(20);
        seekFiles(fileList, new File(dir), isSubDir);
        return fileList;
    }

    /**
     * 检索目录文件
     * @param fileList
     * @param file
     * @param isSubDir：true-检索子目录
     */
    public static void seekFiles(List<String> fileList, File file, boolean isSubDir){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(int i=0; i<files.length; i++){
                if(files[i].isFile()){
                    fileList.add(files[i].getAbsolutePath());
                }else{
                    if(isSubDir){
                        seekFiles(fileList, files[i], isSubDir);
                    }
                }
            }
        }else if(file.isFile()){
            fileList.add(file.getAbsolutePath());
        }else{
            throw new RuntimeException("Unrecognized file path:"+file.getAbsolutePath());
        }
    }

    /**
     * 获取文件名称
     * @param file
     * @Return:String
     * @Time:2017-6-15
     * @Author:Arno
     */
    public static String getFilename(String file){
        int pos = file.lastIndexOf(File.separator);
        if(pos==-1){
            return file;
        }else{
            return file.substring(++pos);
        }
    }

    /**
     * 获取文件名称
     * @Description:
     * @param file
     * @return
     * @Return:String
     * @Time:2017-6-15
     * @Author:Arno
     */
    public static String getFileExtension(String file){
        int pos = file.lastIndexOf(".");
        if(pos==-1){
            return file;
        }else{
            return file.substring(++pos);
        }
    }

    /**
     * 复制文件
     * @param srcFile
     * @param destFile
     */
    public static boolean copyFile(String srcFile, String destFile){
        try{
            org.apache.commons.io.FileUtils.copyFile(new File(srcFile), new File(destFile));
            return true;
        }catch(Exception exp){
            log.error("删除文件出错！", exp);
        }
        return false;
    }

    /**
     * 移动文件
     * @param srcFile
     * @param destFile
     */
    public static boolean moveFile(String srcFile, String destFile){
        try{
            org.apache.commons.io.FileUtils.moveFile(new File(srcFile), new File(destFile));
            return true;
        }catch(Exception exp){
            log.error("删除文件出错！", exp);
        }
        return false;
    }

    /**
     * 删除文件
     * @param file
     */
    public static boolean deleteFile(String file){
        try{
            org.apache.commons.io.FileUtils.forceDelete(new File(file));
            return true;
        }catch(Exception exp){
            log.error("删除文件出错！", exp);
        }
        return false;
    }

}